/*********************************************************************************
 *      Copyright:  (C) 2022 zengwen<1825984037@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  package_data.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(01/08/22)
 *         Author:  zengwen <1825984037@qq.com>
 *      ChangeLog:  1, Release initial version on "01/08/22 14:49:37"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <mosquitto.h>
#include <stdlib.h>

#include "cJSON.h"
#include "get_massage.h"


int package_data( char msg[], char *platform)
{
	cJSON      *root;
	cJSON      *item;
	cJSON      *item2;
	cJSON      *array;
	const char  s[2] = "/";
	char       *temp = NULL;
	char       *humi = NULL;
	float       temper;
	float       humidity;
	char       *cur_time;

	cur_time = strtok(msg,s );
	temp = strtok(NULL,s );
	humi = strtok(NULL,s);
	temper = atof(temp);
	humidity = atof(humi);

	root = cJSON_CreateObject();//创建JSON对象
	item = cJSON_CreateObject();
	item2 = cJSON_CreateObject();
	array = cJSON_CreateArray();//创建JSON数组

	printf ("platform=%s\n",platform);

	if( strcmp(platform,"alicloud") == 0)
	{
		cJSON_AddItemToObject(root,"time",cJSON_CreateString(cur_time));
		cJSON_AddItemToObject(root,"params",item);
		cJSON_AddItemToObject(item,"CurrentTemperature",cJSON_CreateNumber(temper));
	}

	else if(strcmp(platform,"tencentcloud") == 0)
	{
		cJSON_AddItemToObject(root,"type",cJSON_CreateString("update"));
		cJSON_AddItemToObject(item2,"Temperature",cJSON_CreateNumber(temper));
		cJSON_AddItemToObject(item,"reported",item2);
		cJSON_AddItemToObject(root,"state",item);
		cJSON_AddItemToObject(root,"version",cJSON_CreateString("1"));
		cJSON_AddItemToObject(root,"clientToken",cJSON_CreateString("clientToken"));
	}

	else if(strcmp(platform,"huaweicloud") == 0)
	{
		cJSON_AddItemToObject(root,"services",array);
		cJSON_AddItemToArray(array,item);
		cJSON_AddItemToObject(item,"service_id",cJSON_CreateString("Temperature"));
		cJSON_AddItemToObject(item,"properties",item2);
		cJSON_AddItemToObject(item2,"Temperature",cJSON_CreateNumber(temper));
		cJSON_AddItemToObject(item2,"Time",cJSON_CreateString(cur_time));
		cJSON_AddItemToObject(item2,"humidity",cJSON_CreateNumber(humidity));
	}

	memset(msg, 0, 256);
	snprintf(msg, 256, "%s",cJSON_Print(root));

	cJSON_Delete(root);

	return 0;
}

int package_massage(char Data[])
{
	float   temper;
	float	humer;
	char    cur_time[32];

	if(get_temp_hum(&temper,&humer)<0)
	{
		printf("get_temperature failed.\n");
		return -1;
	}

	if(get_time(cur_time)<0)
	{
		printf("get_time failure\n");
		return -2;
	}

	snprintf(Data,256,"%s/%f/%f",cur_time,temper,humer);

	return 0;
}


